package com.st.mall.order.service.Impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.st.mall.order.mapper.IOrderInfoMapper;
import com.st.mall.order.mapper.IOrderProMapper;
import com.st.mall.order.model.OrderInfoModel;
import com.st.mall.order.model.OrderProModel;
import com.st.mall.order.service.IOrderInfoService;

@Service
public class OrderInfoServiceImpl implements IOrderInfoService {

	@Autowired
	private IOrderInfoMapper mapper;
	@Autowired
	private IOrderProMapper proMapper;
	
	Date d = new Date();
	
//	@Autowired
//	private IAddressMapper addrMapper;

	@Override
	public int insert(OrderInfoModel orderInfoModel) {
		// TODO Auto-generated method stub 
		String code = UUID.randomUUID().toString();
		orderInfoModel.setCode(code);
		
		Timestamp date = new Timestamp(d.getTime());
		orderInfoModel.setDate(date);
		
//		AddressModel model = new AddressModel();
//		model.setCode(orderInfoModel.getAddrCode());
//		model = addrMapper.selectId(model);
//		orderInfoModel.setAddrName(model.getAddr1Name()+"-"+model.getAddr1Name()+"-"+model.getAddr1Name()+"-"+model.getAddr4());
		
		return mapper.insert(orderInfoModel);
	}

	@Override
	public int delete(String code) {
		// TODO Auto-generated method stub
		return mapper.delete(code);
	}

	@Override
	public int selectCount(OrderInfoModel orderInfoModel) {
		// TODO Auto-generated method stub
		return mapper.selectCount(orderInfoModel);
	}

	@Override
	public List<OrderInfoModel> selectModel(OrderInfoModel orderInfoModel) {
		// TODO Auto-generated method stub
		return mapper.selectModel(orderInfoModel);
	}
	
	@Override
	public List<OrderInfoModel> selectAll(OrderInfoModel orderInfoModel) {
		// TODO Auto-generated method stub
		List<OrderInfoModel> list = mapper.selectAll(orderInfoModel);
		for(int i=0;i<list.size();i++) {
			double total = 0.0;
			String orderCode = list.get(i).getCode();
			List<OrderProModel> olist = proMapper.selectAll(orderCode);
			for(OrderProModel model:olist) {
				double tmp = model.getPrice()*model.getNumb();
				total +=tmp;
			}
			list.get(i).setTotal(total);
		}
		return list;
	}

	@Override
	public int update(OrderInfoModel orderInfoModel) {
		// TODO Auto-generated method stub
		return mapper.update(orderInfoModel);
	}

	@Override
	public OrderInfoModel selectId(String code) {
		// TODO Auto-generated method stub
		return mapper.selectId(code);
	}

	@Override
	public String add(OrderInfoModel orderInfoModel) {
		// TODO Auto-generated method stub
		String code = UUID.randomUUID().toString();
		orderInfoModel.setCode(code);
		Timestamp date = new Timestamp(d.getTime());
		orderInfoModel.setDate(date);
		int result = mapper.insert(orderInfoModel);

		if(result == 1)
			return orderInfoModel.getCode();
		else
			return "B";
	}
	
	
	
}
